// 当前项目
const project = require('./utils/config/project.js')
// 读取 manifest.json ，修改后重新写入
const fs = require('fs')

const manifestPath = process.env.UNI_INPUT_DIR + '/manifest.json'
let Manifest = fs.readFileSync(manifestPath, { encoding: 'utf-8' })

function replaceManifest(path, value) {
  const arr = path.split('.')
  const len = arr.length
  const lastItem = arr[len - 1]

  let i = 0
  let ManifestArr = Manifest.split(/\n/)

  for (let index = 0; index < ManifestArr.length; index++) {
    const item = ManifestArr[index]
    if (new RegExp(`"${arr[i]}"`).test(item)) ++i;
    if (i === len) {
      const hasComma = /,/.test(item)
      ManifestArr[index] = item.replace(new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`), `"${lastItem}": ${value}${hasComma ? ',' : ''}`)
      break;
    }
  }

  Manifest = ManifestArr.join('\n')
}

// 修改应用名称
replaceManifest('name', `"${project.APP_NAME}"`)

// 修改应用版本
const versionName = `${project.APP_VERSION}`
const versionCode = versionName.replace(/\./g, '')
replaceManifest('versionName', `"${versionName}"`)
replaceManifest('versionCode', `"${versionCode}"`)

// 修改图标
replaceManifest('app-plus.distribute.icons.android.hdpi', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/72x72.png"')
replaceManifest('app-plus.distribute.icons.android.xhdpi', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/96x96.png"')
replaceManifest('app-plus.distribute.icons.android.xxhdpi', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/144x144.png"')
replaceManifest('app-plus.distribute.icons.android.xxxhdpi', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/192x192.png"')
replaceManifest('app-plus.distribute.icons.ios.appstore', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/1024x1024.png"')
replaceManifest('app-plus.distribute.icons.ios.iphone.app@2x', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/120x120.png"')
replaceManifest('app-plus.distribute.icons.ios.iphone.app@3x', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/180x180.png"')
replaceManifest('app-plus.distribute.icons.ios.iphone.notification@2x', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/40x40.png"')
replaceManifest('app-plus.distribute.icons.ios.iphone.notification@3x', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/60x60.png"')
replaceManifest('app-plus.distribute.icons.ios.iphone.settings@2x', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/58x58.png"')
replaceManifest('app-plus.distribute.icons.ios.iphone.settings@3x', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/87x87.png"')
replaceManifest('app-plus.distribute.icons.ios.iphone.spotlight@2x', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/80x80.png"')
replaceManifest('app-plus.distribute.icons.ios.iphone.spotlight@3x', '"unpackage/res/icons/'+`${project.PINYIN_NAME}`+'/120x120.png"')

// 修改启动图
replaceManifest('app-plus.distribute.splashscreen.android.hdpi', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.android.xhdpi', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.android.xxhdpi', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.portrait-896h@3x', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.landscape-896h@3x', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.portrait-896h@2x', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.landscape-896h@2x', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.iphonex', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.iphonexl', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.retina55', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.retina55l', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.retina47', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.retina47l', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.retina40', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.retina40l', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')
replaceManifest('app-plus.distribute.splashscreen.ios.iphone.retina35', '"unpackage/res/imgs/'+`${project.PINYIN_NAME}`+'/bg.png"')

fs.writeFileSync(manifestPath, Manifest, {
  "flag": "w"
})

// 获取所有路由信息
const TransformPages = require('uni-read-pages')
const {webpack} = new TransformPages()
module.exports = {
	configureWebpack: {
		plugins: [
			new webpack.DefinePlugin({
				ROUTES: webpack.DefinePlugin.runtimeValue(() => {
					const tfPages = new TransformPages({
						includes: ['path']
					});
					return JSON.stringify(tfPages.routes)
				}, true)
			})
		]
	}
}